package main

/*
#cgo CFLAGS: -DPNG_DEBUG=1 -I./mcu_src
#cgo LDFLAGS: -L./ -lmcuRuntime
#include "mcu_src/init.h"
#include "mcu_src/runnable.h"
*/
import "C"

import (
		"fmt"
		"runtime/debug"
		"time"
		"runtime"
		"strings"
		)
var pause bool
func main(){
	pause=false
	info, ok := debug.ReadBuildInfo()
	if ok {
		// Print all fields of debug.Module
		fmt.Printf("Module:\n")
		fmt.Printf("  Path: %s\n", info.Path)
		fmt.Printf("  GoVersion: %s\n", info.GoVersion )
		for _, setting := range info.Settings {
			if setting.Value != "" {
				fmt.Printf("key: %s  value: %s\n", setting.Key, setting.Value)
			}
    	}
	} else {
		fmt.Println("Unable to retrieve build information.")
	}
	funInit()
	go fun1ms()
	go fun10ms()
	go fun20ms()
	go fun50ms()
	go fun100ms()
	var input string
	var lastInput string
	for {
		fmt.Print("Enter a command ('exit' to quit): ")
		fmt.Scanln(&input)
		if input=="a"{
			input=lastInput
		}
		switch strings.ToLower(strings.TrimSpace(input)){
		case "exit":
			return
		case "threads":
			numGoroutines := runtime.NumGoroutine()
			fmt.Printf("Number of goroutines: %d\n", numGoroutines)
		case "pause":
			pause = true
		case "run":
			pause = false
		case "printmcuinfo":
			C.printMcuInfo()
		default:
			fmt.Println("Invalid command. valid command:exit threads pause run printmcuinfo",lastInput)
		}
		lastInput=input
	}
	
	funDeInit()
	mcuRunTimes:=C.mcuDeInitTimeMillis-C.mcuInitTimeMillis;
	fmt.Printf("MCU RUN TIMES:%d ms\n",mcuRunTimes)
}
func funInit(){
	C.mcuInit()
}
func funDeInit(){
	C.mcuDeInit()
}
func fun1ms(){
	for{
		time.Sleep(1 * time.Millisecond)
		C.runnable1ms()
		for{
			if pause==true {
				time.Sleep(1 * time.Millisecond)
			}else{
				break
			}
		}
	}
}
func fun10ms(){
	for{
		time.Sleep(10 * time.Millisecond)
		C.runnable10ms()
		for{
			if pause==true {
				time.Sleep(1 * time.Millisecond)
			}else{
				break
			}
		}
	}
}
func fun20ms(){
	for{
		time.Sleep(20 * time.Millisecond)
		C.runnable20ms()
		for{
			if pause==true {
				time.Sleep(1 * time.Millisecond)
			}else{
				break
			}
		}
	}
}
func fun50ms(){
	for{
		time.Sleep(50 * time.Millisecond)
		C.runnable50ms()
		for{
			if pause==true {
				time.Sleep(1 * time.Millisecond)
			}else{
				break
			}
		}
	}
}
func fun100ms(){
	for{
		time.Sleep(100 * time.Millisecond)
		C.runnable100ms()
		for{
			if pause==true {
				time.Sleep(1 * time.Millisecond)
			}else{
				break
			}
		}
	}
}